import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:tree_hole/treeHole/TreeHoleList.dart';
import 'package:tree_hole/treeHole/SingleTreeHolePage.dart';
import 'package:tree_hole/treeHole/TreeHolePage.dart';

class ReplyPage extends StatefulWidget {
  final String token;
  int articleId,commentId;
  ReplyPage(this.token,this.articleId,{this.commentId,Key key}) : super(key: key);

  @override
  _ReplyPage createState() => new _ReplyPage();
}
class _ReplyPage extends State<ReplyPage> {
  String content;
  String result;
  Widget build(BuildContext context){
    return Scaffold(
      appBar: AppBar(
        title: Container(
          child: Text('回复',
            style: TextStyle(
                color: Colors.black,
                letterSpacing: 5
            ),
          ),
        ),
        centerTitle:true,
        backgroundColor: Colors.white,
        //titleSpacing: 20,
        leading: IconButton(
          icon:Icon(Icons.arrow_back,color: Colors.black),
          onPressed: () {Navigator.pop(context);},
        ),
        actions: [
          IconButton(icon: Icon(Icons.check,color: Colors.black87,),
            onPressed: () async {
              if(content==null){
                result='内容不能为空';
              }
              else {
                if(widget.commentId!=0)
                result=await TreeHoleList.addComment(widget.token, widget.articleId, content,commentId: widget.commentId);
                else
                  result=await TreeHoleList.addComment(widget.token, widget.articleId, content);
              if(result=='回复成功'){
                Navigator.of(context).pop('pop');

                //Navigator.pushAndRemoveUntil(context, new MaterialPageRoute(builder: (context) => SingleTreeHolePage(token:widget.token,article:widget.article,result: result,)), ModalRoute.withName('SingleTreeHolePage'));
                //Navigator.popUntil(context, ModalRoute.withName('SingleTreeHolePage'));
                //Navigator.pushReplacement(context, new MaterialPageRoute(builder: (context) => SingleTreeHolePage(widget.token,widget.article,result: result,)));
              }
              }
              Fluttertoast.showToast(
                  msg: result,
                  toastLength: Toast.LENGTH_SHORT,
                  gravity: ToastGravity.BOTTOM,
                  timeInSecForIosWeb: 1,
                  backgroundColor: Colors.green[100],
                  textColor: Colors.black,
                  fontSize: 16.0
              );
            },
          )
        ],
      ),
      body: Container(
        height: double.infinity,
        width: double.infinity,
        padding: EdgeInsets.all(10),
        child: ListView(
          //mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 10,),
            TextField(
              minLines: 1,
              maxLines: 1000,
              keyboardType: TextInputType.multiline,
              cursorColor:Colors.black,
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.all(10),
                  hintText: '内容',
                  filled: true,
                  fillColor: Colors.transparent,
                  focusedBorder: OutlineInputBorder(
                      borderSide: BorderSide(
                        color: Colors.black,
                      )
                  ),
                  border: OutlineInputBorder(
                    borderSide: BorderSide(
                      color: Colors.black,
                    ),
                  ),
                isCollapsed: true,
              ),
              onChanged: (value){setState(() {
                content=value;
                print(content);
              });
              },
            ),
          ],
        ),
        decoration: BoxDecoration(
            image: new DecorationImage(
                fit: BoxFit.cover,
                image: new AssetImage('images/treeHole.jpg'))),
      ),
    );
  }
}